/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.gui.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import ui.WindowManager;
import ui.gui.documents.AbstractDocument;
import ui.gui.documents.EditorFactory;
import ui.gui.documents.toolbox.Workspace;

/**
 *
 * @author stephane
 */

public class LoadModelAction extends AbstractAction
{
    private static final long serialVersionUID = 1L;
    private boolean _displayChoose = false;
    public LoadModelAction(JFrame parent, boolean displayChoose)
    {
        super(displayChoose ? "Load and parse model ..." : "Parse current model", new javax.swing.ImageIcon(LoadModelAction.class.getResource("/resources/parser.png")));
        this._displayChoose = displayChoose;
        putValue(SHORT_DESCRIPTION, displayChoose ? "Load and parse an alatarica model" : "Parse current altarica model"); 
    }
    
    
    private void loadNewDocument() {
        JFileChooser fileChooser = new JFileChooser();
        
        for (int i = 0; i < EditorFactory.getInstance().getExtensionFilter().length;i++) {	
	        fileChooser.addChoosableFileFilter(new DefaultFileFilter(EditorFactory.getInstance().getExtensionFilter()[i], EditorFactory.getInstance().getDescriptionFilter()[i]));
	
        }
        fileChooser.showOpenDialog(null);
        File file = fileChooser.getSelectedFile();
        if (file !=null && file.isFile())
        {
            AbstractDocument document = EditorFactory.getInstance().createFromFile(file);
            Workspace.getInstance().AddDocument(document);
            if (document != null) {
                common.Commands.RunCommand("load "+ document.getFile().getAbsolutePath(), true);
             
            }
        }
        
    }
    
    private void loadCurrentDocument() {
        AbstractDocument document = WindowManager.getInstance().getSelectedDocument();
        if (document != null) {
            common.Commands.RunCommand("load "+ document.getFile().getAbsolutePath(), true);
            
        }
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        if (this._displayChoose) {
            loadNewDocument();
        }
        else {
            loadCurrentDocument();
        }
    }
    
}